package com.example.springboot.controller;

import com.example.springboot.pojo.Count;
import com.example.springboot.pojo.Infos;
import com.example.springboot.service.CountService;
import com.example.springboot.service.InfosService;
import com.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController

public class InfosController {
    @Autowired
    InfosService infosService;
    @Autowired
    CountService countService;
    @Autowired
    UserService userService;


    @RequestMapping("/show")
    @CrossOrigin
    public List<Infos> findAll() {
        List<Infos> lists = infosService.findAll();
        return lists;
    }


    @RequestMapping("/message")
    @CrossOrigin
    public Map<String, Object> findAllM() {
        Count count = countService.findAll();
        int temp = infosService.countInfos();
        Map<String, Object> map = new HashMap<>();
        map.put("count",count);
        map.put("pl",temp);
        return map;
    }

    @RequestMapping("/updateDZ")
    @CrossOrigin
    public void insertDianZan() {
        int row = countService.updateDianZan();
    }

    @RequestMapping("/cancelDrag")
    @CrossOrigin
    public void deleteDianZan() {
        int row = countService.cancelDrag();
    }

    @RequestMapping("/commit")
    @CrossOrigin
    public String plCommit(@RequestBody Infos infos) {

        Infos info = infos;
        info.setUserid(userService.findUserIdOnLine().getId());
        int row = infosService.insertInfos(info);
        countService.updatePingLUn();
        return "ok";
    }
}
